Comm Operator v4.5 supports Ruby Script in “Auto Send” function. Ruby Script will be run automatically.
Here are the steps to add Ruby Script to Auto Send Rule in Comm Operator.
1. Valid Ruby interpreter has to be installed. It can be downloaded from http://www.ruby-lang.org/en/
2. Click menu [Tools]->[Options]->[Script] page to set the path of Ruby interpreter “Ruby.exe” correctly.
3. Click menu [Edit]->[Auto Send] to open the “Auto Send Rule Setting” dialog.
4. Click “New” button to show “Select Rule Type” dialog.
5. Select “Ruby Script” and Click “OK”. The “Ruby Script Rule” dialog will be shown.
6. Choose your Ruby script. Click “View” button will open the script with Windows Notepad.
Tip:
- If the checkbox”Always” is selected, the script will be run again and again. Otherwise, it won’t be called once it is matched.
- The Comm Operator comes with a demo Ruby script called “Sample.rb”. The file can be find in Comm Operator’s install folder. The default folder is “C:\Program Files\Serial Port Tool\Comm Operator\” or “C:\Program Files(x86)\Serial Port Tool\Comm Operator\” for 64 bits OS.
7. Click OK to finish adding Ruby Script Rule.
How does it work?
Comm Operator process the “Auto Send” in a separate thread. The script will be called in every loop. The script handles the events and determine if there are data need to be sent in “Auto Send”. “Matched” means there is data come back from script and need to be sent.
When a script is called, Comm Operator write the event type as well as data of this event in STDIN. The event type could be one of the following.
“DataReceived”, raised when data received in Comm Operator in current connection. The data received are followed after the event string in hex string format.
“Start”, raised when the auto send starts.
“Pause”, raised when the auto send pauses.
“Resume”, raised when the auto send resumes.
“Loop”, raised in every loop of the auto send.
Only “DataReceived” has data, the data are hex strings separated by one space. Like “61 62 63 64 “ represents the data “ABCD”. The whole package write to STDIN in this case will be
DataReceived,61 62 63 64
For other event types, the package only has only the type name followed with a “,”.
The package ends with a CR. In Ruby script, the following code will read the whole data package.
str = gets()
As the event and data are separated by “,”, it can be parsed easily by the following code.
data = str.split(‘,’)
data[0] is the event type and data[1] contains the data related.
The data send back to Comm Operator is written to STDOUT, the data are formatted in hex string.
Here is the sample code that echo all data received.
if(data[0] == "DataReceived") then
puts data[1]
end
Here is the help function for creating hex string from ascii string.
##Convert each ASCII character to a two-digit hex number seperated with a space char
## like "ABCD" to "65 66 67 68 "
def ascii_to_hex (str)
hexStr = ""
str.each_byte{
|c| hexStr += " %02x"%c
}
return hexStr
end
Here is the sample code to send string “Hello” to Comm Operator.
puts ascii_to_hex(“Hello”)
Here is the help function for converting hex string to ascii string. It is useful to parse the data comes from Comm Operator.
##Convert each two-digit hex number back to an ASCII character.
## the string format like "A5 B3 ", there is a space char between two hex numbers
## like "65 66 67 68 " to "ABCD"
def hex_to_ascii (str)
bytesStr = str.split(‘ ‘)
bytes = Array.new()
bytesStr.each{|x| bytes.push(x.hex) }
str = bytes.pack("c*")
return str;
end
Here is the code of “Sample.rb”.
## To make this script run, please setup the Ruby path correctly in "Tools->Options"
## sample ruby script to Comm Operator
## Comm Operator will write data to STDIN
## Format
## Type, hex hex hex …
## Type: String that represent the type of event, can be
## "DataReceived"
## "Start"
## "Pause"
## "Resume"
## If the type is "DataReceived", it will follow byte "," and then the data in hex string.
## For example, if the Comm Operator recieved string "abcd", it will call script with data like below
## DataReceived, 61 62 63 64##Convert each ASCII character to a two-digit hex number seperated with a space char
## like "ABCD" to "65 66 67 68 "
def ascii_to_hex (str)
hexStr = ""
str.each_byte{
|c| hexStr += " %02x"%c
}
return hexStr
end##Convert each two-digit hex number back to an ASCII character.
## the string format like "A5 B3 ", there is a space char between two hex numbers
## like "65 66 67 68 " to "ABCD"
def hex_to_ascii (str)
bytesStr = str.split(‘ ‘)
bytes = Array.new()
bytesStr.each{|x| bytes.push(x.hex) }
str = bytes.pack("c*")
return str;
end## Read data send from Comm Operator
str = gets()
## Parse data, the data contains two parts. The first part is event type, the seond part are data in hex string
data = str.split(‘,’)## test if it is the "DataReceived" event
if(data[0] == "DataReceived") then
## echo back the data received, replace it with you own code
puts data[1]
end## test if it is the "Start" event
if(data[0] == "Start") then
## send string "Start", replace it with you own code
puts ascii_to_hex("Start")
end## test if it is the "Pause" event
if(data[0] == "Pause") then
## send string "Pause", replace it with you own code
puts ascii_to_hex("Pause")
end## test if it is the "Resume" event
if(data[0] == "Resume") then
## send string "Resume", replace it with you own code
puts ascii_to_hex("Resume")
end## test if it is the "Loop" event
if(data[0] == "Loop") then
## process data here is not recommended.
#sleep 1
#puts ascii_to_hex("Loop")
end
See Also